package com.luoying.itew.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.luoying.itew.R;
import com.luoying.itew.bean.MonitorBean;
import com.luoying.itew.util.StateContext;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Created by lufan on 2018/6/4
 * Class describe
 */
public class WaitListAdapter extends RecyclerView.Adapter<WaitListAdapter.WaitViewHolder> {
    private List<MonitorBean> list;
    private WaitListOnItemClickListener listener;
    public Context context;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

    public void setListener(WaitListOnItemClickListener listener) {
        this.listener = listener;
    }

    public WaitListAdapter(List<MonitorBean> list) {
        this.list = list;
    }

    @Override
    public WaitViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wait_layout, parent, false);
        return new WaitViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WaitViewHolder holder, int position) {
        MonitorBean bean = list.get(position);
        holder.itemWaitName.setText(bean.getMonitorPointName());
        String time = "";
        if(!TextUtils.isEmpty(bean.getWarnTime())){
            time = String.format(context.getString(R.string.wait_warn_tiem), bean.getWarnTime());
        }
        holder.itemWaitTime.setText(time);
        StateContext stateContext = new StateContext(bean.getMonitorPointStatus(),bean.getWarnCoefficient());
        holder.itemWaitState.setText(String.format("预警状态：%s", stateContext.levelStr()));
        holder.itemWaitState.setTextColor(context.getResources().getColor(stateContext.levelColor()));
        holder.itemWaitInfo.setText("待办内容：应密切关注，加强巡查频率。");
        holder.rootView.setOnClickListener((v -> {
            if(listener!=null){
                listener.onClick(bean);
            }
        }));
        holder.itemWaitShare.setOnClickListener((v -> {
            if(listener!=null){
                listener.onShareWx(bean);
            }
        }));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class WaitViewHolder extends RecyclerView.ViewHolder {
        TextView itemWaitName;
        TextView itemWaitTime;
        TextView itemWaitState;
        TextView itemWaitInfo;
        ImageView itemWaitShare;
        View rootView;
        public WaitViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            itemWaitName = (TextView) itemView.findViewById(R.id.item_wait_name);
            itemWaitTime = (TextView) itemView.findViewById(R.id.item_wait_time);
            itemWaitState = (TextView) itemView.findViewById(R.id.item_wait_state);
            itemWaitInfo = (TextView) itemView.findViewById(R.id.item_wait_info);
            itemWaitShare = itemView.findViewById(R.id.item_wait_share_iv);
        }
    }

    /**
     * 列表点击事件
     */
    public interface WaitListOnItemClickListener {
        void onClick(MonitorBean bean);
        void onShareWx(MonitorBean bean);
    }
}
